package io.chenglicun.modules.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import dm.jdbc.util.StringUtil;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.BaseServiceImpl;
import io.chenglicun.common.user.UserDetail;
import io.chenglicun.common.utils.ConvertUtils;
import io.chenglicun.common.utils.Result;
import io.chenglicun.modules.bus.dao.BusMomentLikeDao;
import io.chenglicun.modules.bus.dto.BusMomentLikeDTO;
import io.chenglicun.modules.bus.entity.BusMomentLikeEntity;
import io.chenglicun.modules.bus.service.BusMomentLikeService;
import io.chenglicun.modules.security.user.SecurityUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 业务_村民留言版/乡情圈_点赞
 *
 * @author LYB jssqsylyb@163.com
 * @since 1.0 2023-09-07
 */
@Service
public class BusMomentLikeServiceImpl extends BaseServiceImpl<BusMomentLikeDao, BusMomentLikeEntity> implements BusMomentLikeService {


    @Autowired
    private BusMomentLikeDao busMomentLikeDao;

    public QueryWrapper<BusMomentLikeEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<BusMomentLikeEntity> wrapper = new QueryWrapper<>();

        return wrapper;
    }


    @Override
    public PageData<BusMomentLikeDTO> page(Map<String, Object> params) {
        IPage<BusMomentLikeEntity> page = baseDao.selectPage(
                getPage(params, "like_time", false),
                getWrapper(params)
        );

        return getPageData(page, BusMomentLikeDTO.class);
    }

    @Override
    public BusMomentLikeDTO get(Long id) {
        BusMomentLikeEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, BusMomentLikeDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BusMomentLikeDTO dto) {
        UserDetail user = SecurityUser.getUser();
        dto.setUserId(user.getId());
        dto.setUserName(user.getUsername());
        dto.setRealName(user.getRealName());
        dto.setLikeTime(new Date());

        BusMomentLikeEntity entity = ConvertUtils.sourceToTarget(dto, BusMomentLikeEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result like(BusMomentLikeDTO dto) {
        UserDetail user = SecurityUser.getUser();
        BusMomentLikeDTO obj = busMomentLikeDao.selectObj(user.getId(),dto.getMomentId());
        if(obj!=null){
            return new Result().error("已点赞!");
        }
        dto.setUserId(user.getId());
        dto.setUserName(user.getUsername());
        dto.setRealName(user.getRealName());
        dto.setLikeTime(new Date());
        BusMomentLikeEntity entity = ConvertUtils.sourceToTarget(dto, BusMomentLikeEntity.class);
        try {
            insert(entity);
        }catch (DuplicateKeyException e){
            return new Result().error("已点赞!");
        }

        return new Result();
    }


    @Override
    public void cancellationLike(Long id) {
        UserDetail user = SecurityUser.getUser();
        BusMomentLikeDTO dto = busMomentLikeDao.selectObj(user.getId(),id);
        if(dto!=null){
            baseDao.deleteById(dto.getId());
        }

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BusMomentLikeDTO dto) {
        BusMomentLikeEntity entity = ConvertUtils.sourceToTarget(dto, BusMomentLikeEntity.class);

        updateById(entity);
    }

    @Override
    public void delete(Long[] ids) {
        deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<BusMomentLikeDTO> list(Map<String, Object> params) {
        List<BusMomentLikeEntity> entityList = baseDao.selectList(getWrapper(params).orderByDesc("like_time"));

        return ConvertUtils.sourceToTarget(entityList, BusMomentLikeDTO.class);
    }


}